<?php
namespace app\administrative\controller;
use app\common\model\User;
use app\common\model\Node;

/**
 * 用户组管理
 * @author yq
 *
 */
class Authgroup extends Base{
   
    /**
     * 用户组列表
     * @return mixed|string
     */
    public function index(){
        $user = new User();
        $node = new Node();
        
        $where['association_id'] = session($this->set['db_name'].'a_id');
        $auth_group = $user->getAllAuthGroup($this->set['auth_group'],$where);
        //dump($auth_group);exit;
        //获取全部节点
        $rule = $node->getAllMenu(['status'=>1],$this->set['auth_rule']);
        
        $parent = []; //父类
        foreach($rule as $key=>$vo){
            if($vo['pid'] == 0){
                $parent[] = $vo;
                unset($rule[$key]);
            }
        }
        foreach ($parent as $key => $val){
            foreach ($rule as $k => $v){
                if($v['pid'] == $val['id']){
                    $parent[$key]['child'][] = $v; 
                    unset($rule[$k]);
                }
            }
        }
        foreach ($parent as $key => $val){
            if(!empty($parent[$key]['child'])){
                foreach ($parent[$key]['child'] as $k => $v){
                    foreach ($rule as $kk => $vv){
                        if($v['id'] ==  $vv['pid']){
                            $parent[$key]['child'][$k]['levels'][] = $vv;
                        }
                    }
                }
            }
        }
        $this->assign(['data'=>$auth_group,'rule'=>$parent]);
        return $this->fetch();
    }
    
    
    /**
     * 修改用户组状态
     */
    public function updateStatus(){
        if(request()->isPost()){
            $id = input("param.id");
            $node = new Node();
            $where = ['id'=>$id];
            $db_name = $this->set['auth_group'];
            //查询用户组数据
            $auth_group= $node->getOneAuthGroup($db_name,$where);
            if($auth_group['status'] == 1){
                $update =  $node->modify($where, ['status'=>0], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已禁止']);
            }else if($auth_group['status'] == 0){
                $update =  $node->modify($where, ['status'=>1], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已开启']);
            }
        }
    }
    
    
    /**
     * 添加用户组
     */
    public function add(){
        if(request()->isPost()){
            $data = $this->request->post();
            $node= new Node();
            //组装入库数据
            if(empty($data['status'])){
                $data['status'] = 0;
            }
            //添加关联
            $data['association_id'] = session($this->set['db_name'].'a_id');
            $add = $node->AddAuthgroup($this->set['auth_group'], $data);
            if($add){
                return json(['code'=>1,'msg'=>'添加成功','data'=>url('Authgroup/index')]);
            }else{
                return json(['code'=>2,'msg'=>'添加失败','data'=>'']);
            }
        }
        return $this->fetch();
    }
    
    
    
    /**
     * 编辑用户组
     */
    
    public function edit(){
        $node = new Node();
        if(request()->isPost()){
            $data = $this->request->post();
            $where['id'] = $data['id'];
            unset($data['id']);
            $res = $node->modify($where, $data, $this->set['auth_group']);
            if($res){
                return json(['code'=>1,'msg'=>'修改成功','data'=>url('Authgroup/index')]);
            }else{
                return json(['code'=>2,'msg'=>'修改失败','data'=>'']);
            } 
        }
        $id = input("param.id");
        $auth_group =  $node->getOneAuthGroup($this->set['auth_group'],['id'=>$id]);
        $this->assign('data',$auth_group);
        return $this->fetch();
    }
    
    
    
   /**
    * 获取用户组权限节点
    */
    
    
    public function AjaxAuthority(){
        if(request()->isPost()){
            $data = $this->request->post();
            $node = new Node();
            $auth_group = $node->getOneAuthGroup($this->set['auth_group'], ['id'=>$data['id']]);
            $rules = explode(',', $auth_group['rules']);
            return json(['code'=>1,'msg'=>'','data'=>$rules]);
        }
    }
    
    
    
    /**
     * 添加用户组节点
     */
    public function AddRules(){
        if(request()->isPost()){
            $data = $this->request->post();
            $node = new Node();
            $rules = empty($data['rules']) ? "":implode(',',$data['rules']);
            $update =  $node->modify(['id'=>$data['id']], ['rules'=>$rules], $this->set['auth_group']);
            if($update){
                return json(['code'=>1,'msg'=>'添加节点成功','data'=>url('Authgroup/index')]);
            }else{
                return json(['code'=>2,'msg'=>'添加节点失败','data'=>'']);
            }
        }
    }
    
    
    
    
    
    
    
}